package com.example.mykotlinapp

import android.app.Activity
import android.content.Context
import java.lang.StringBuilder
import kotlin.reflect.full.memberProperties

/**
 *
 *
 * @author zhudeshuai
 * @since 2019/8/6
 */

//反射

class Teacher(val name: String, val age: Int)

fun test170() {
    val teacher = Teacher("Jack", 18)
    val kClass = teacher.javaClass.kotlin
    println(kClass.simpleName)
    kClass.memberProperties.forEach {
        println(it.name)
    }
}


//DSL

fun buildString(builderAction: (StringBuilder) -> Unit): String {
    val sb = StringBuilder()
    builderAction(sb)
    return sb.toString()
}


fun buildStringAdvance(builderAction: StringBuilder.() -> Unit): String {
    val sb = StringBuilder()
    sb.builderAction()
    return sb.toString()
}

fun buildStringAdvanceNext(builderAction: StringBuilder.() -> Unit): String =
    StringBuilder().apply(builderAction).toString()


fun test171() {
    val s1 = buildString {
        it.append("hello,")
        it.append("world")
    }
    val s2 = buildStringAdvance {
        append("hello,")
        append("world")
    }
}

val appendExcl: StringBuilder.() -> Unit = { this.append("!") }


open class Tag(val name: String) {
    private val children = mutableListOf<Tag>()

    protected fun <T : Tag> doInit(child: T, init: T.() -> Unit) {
        child.init()
        children.add(child)
    }


    override fun toString(): String = "<$name>${children.joinToString("")}</$name>"
}

fun table(init: TABLE.() -> Unit) = TABLE().apply(init)

class TABLE : Tag("table") {
    fun tr(init: TR.() -> Unit) = doInit(TR(), init)
}

class TR : Tag("tr") {
    fun td(init: TD.() -> Unit) = doInit(TD(), init)
}

class TD : Tag("td")

fun createTable() =
    table {
        tr {
            td {

            }
        }
    }


//Anko:动态创建Android UI

fun Activity.showAreYouSureAlert(process: () -> Unit) {

}



private fun main() {
    println(createTable())
}



